﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using STHPMS1.Common.C_Extension;
using STHPMS1.Common.C_Enum;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/oripv_anagroup")]
    public class Dm_Oripv_AnagroupPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "Getorigpv_groupByPage")]
        public IHttpActionResult Get(int page = 1, int rows = 10)
        {
            var data = this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Oripv_Anagroup>(null, null).ToArray();
            if (data == null) return NotFound();

            
            var result =from oripv in data
                        orderby oripv.Pvmttype_Dictid,oripv.Surf_Thickmin
                        select new { type = ((PavementTypeEnum)oripv.Pvmttype_Dictid).GetEnumDescription(),
                                    thick = FormatCm(oripv.Surf_Thickmin, oripv.Surf_Thickmax),
                                    submodu = FormatCm(oripv.Submodumin, oripv.Submodumax),
                                    formula = FormatFormula(oripv.Formulatype,oripv.Formulacoeff1),
                                     Ansgroup_Id = oripv.Ansgroup_Id,
                                     Pvmttype_Dictid=oripv.Pvmttype_Dictid,
                                     Surf_Thickmin = oripv.Surf_Thickmin,
                                     Surf_Thickmax = oripv.Surf_Thickmax,
                                     Submodumin = oripv.Submodumin,
                                     Submodumax = oripv.Submodumax,
                                     Formulatype = oripv.Formulatype,
                                     Formulacoeff1 = oripv.Formulacoeff1,
                                     comment="y:路面性能指标;x:距最近一次大修的时间（年）"
                        }
                        ;

            return Ok(result);
        }

        private string FormatFormula(int? type, double? coeff1,double? coeff2=null,double? coeff3=null)
        {
            string result = "";
            FormulaEnum f=(FormulaEnum)type;
            if (f==FormulaEnum.Equation1)
            {
                result = string.Format("y=100-{0}x",coeff1);
            }
            else if (f == FormulaEnum.Equation2)
            {
                result = string.Format("y={0}-{1}x", coeff1, coeff2);
            }
            else if (f == FormulaEnum.Equation3)
            {
                result = string.Format("y={0}-{1}x-{2}x²", coeff1, coeff2,coeff3);
            }
            return result;

        }

        private string FormatCm(double? min,double? max)
        {
            string result = "";
            if( (min == null || min == 0 ) && (max==null || max==0))
                result = "";
            else if (min == null || min == 0)
                result = string.Format("<{0}", max);
            else if (max == null || max == 100)
                result = string.Format(">{0}", min);
            else
                result = string.Format("{0}-{1}", min, max);
            return result;

        }

        [Route("")]
        public HttpResponseMessage Post(Dm_Oripv_Anagroup entity)
        {
            this.Scene.Bll.Dm_Oripv_Anagroup.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getorigpv_groupByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("after_s")]
        public HttpResponseMessage PostAfter(Dm_Oripv_Formula entity)
        {
            this.Scene.Bll.Dm_Oripv_Anagroup.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getorigpv_groupByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public void Delete(Dm_Oripv_Anagroup entity)
        {
            this.Scene.Bll.Dm_Oripv_Anagroup.Remove(entity);
        }

        [Route("after_d")]
        public void DeleteAfter(Dm_Oripv_Formula entity)
        {
            this.Scene.Bll.Dm_Oripv_Anagroup.Remove(entity);
        }

         [Route("group")]
        public dynamic GetModelGroup()
        {
            return this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Oripv_Anagroup>(null, null);
        }

         [Route("line")]
         public IHttpActionResult GetLine()
         {
             var data = this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Oripv_Formula>(p => p.Modelmode == ModelFormEnum.Line.GetEnumValue(), null).ToArray();
             if (data == null) return NotFound();


             var result = from oripv in data
                          orderby oripv.Measure, oripv.Regre_Index
                          select new
                          {
                              type = ((MainTypeEnum)oripv.Measure).GetEnumDescription(),
                              coeff = ((RegressionIndicatorsEnum)oripv.Regre_Index).GetEnumDescription(),
                              formula = FormatFormula(oripv.Formulatype, oripv.Formulacoeff1, oripv.Formulacoeff2),
                              Ansgroup_Id = oripv.Ansgroup_Id,
                              Measure = oripv.Measure,
                              Regre_Index = oripv.Regre_Index,
                              Formulatype = oripv.Formulatype,
                              Formulacoeff1 = oripv.Formulacoeff1,
                              Formulacoeff2 = oripv.Formulacoeff2,
                              Formulacoeff3 = oripv.Formulacoeff3,
                              Formulacoeff4 = oripv.Formulacoeff4,
                              Formulacoeff5 = oripv.Formulacoeff5,
                              Modelmode = oripv.Modelmode,
                              comment = "y:路面性能指标;x:距最近一次养护的时间（年）"
                          }
                         ;

             return Ok(result);
         }

         [Route("tab")]
         public IHttpActionResult GetTab()
         {
             var data = this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Oripv_Formula>(p => p.Modelmode == ModelFormEnum.Tab.GetEnumValue(), null).ToArray();
             if (data == null) return NotFound();


             var result = from oripv in data
                          orderby oripv.Measure, oripv.Regre_Index
                          select new
                          {
                              type = ((MainTypeEnum)oripv.Measure).GetEnumDescription(),
                              coeff = ((RegressionIndicatorsEnum)oripv.Regre_Index).GetEnumDescription(),
                              formula = FormatFormula(oripv.Formulatype, oripv.Formulacoeff1, oripv.Formulacoeff2, oripv.Formulacoeff3),
                              Ansgroup_Id = oripv.Ansgroup_Id,
                              Measure = oripv.Measure,
                              Regre_Index = oripv.Regre_Index,
                              Formulatype = oripv.Formulatype,
                              Formulacoeff1 = oripv.Formulacoeff1,
                              Formulacoeff2 = oripv.Formulacoeff2,
                              Formulacoeff3 = oripv.Formulacoeff3,
                              Formulacoeff4 = oripv.Formulacoeff4,
                              Formulacoeff5 = oripv.Formulacoeff5,
                              Modelmode = oripv.Modelmode,
                              comment = "y:路面性能指标;x:距最近一次养护的时间（年）"
                          }
                         ;

             return Ok(result);
         }

    }
}